import {
	DOWNLOAD_URL
} from '@/constant/index.js'

// 隐藏手机号码第四位到第七位
export const hiddenfourthToSeventhPhoneNum = (str) => {
	return str.replace(str.substring(3, 8), ' **** ')
}

// 将图片数组整理成符合后端图片地址字符串
export const mapFileListToPicString = (arr) => {
	let picArr = arr.map(item => item.filePath)
	return picArr.join(',')
}

// 时间格式化
export const timeFormat = (date, format = "yyyy-mm-dd hh:MM:ss") => {
	return uni.$u.timeFormat(date, format)
}

// 将日期拆分成年月日和时分 或 年月日和时分秒
export const dateMapTwoVar = (date, format = "yyyy-mm-dd hh:MM") => {
	const dateResult = timeFormat(date, format)
	const firstVar = dateResult.substr(0, 10)
	const lastVar = dateResult.substr(11)
	return [firstVar, lastVar]
}

/**
 * @description 将图片id字符串转换成图片url数组
 * @param str 图片id字符串
 */
export const mapPicStrToImgUrlArr = (str) => {
	let arr = []
	// if (str !== '') arr = str.split(',').map(item => `${DOWNLOAD_URL}${item}`)
	if (str !== '') arr = str.split(',').map(item => `${item}`)

	return arr
}

/**
 * @description 将后端返回的对象数据映射到数组元素上
 * @param targetArr 目标数组
 * @param detailInfo 后端返回对象数据
 */

export const mapDataToCardCellList = (targetArr, detailInfo) => {
	for (const item of targetArr) {
		if (item.valueType === 'PIC') item.value = detailInfo[item.field] || ""
		item.value = detailInfo[item.field] === '' ? '/' : detailInfo[item.field]
	}
}

export const getCurrentDateAndDaysAgo = (daysAgo) => {
	let obj
	const date = new Date()
	const month = uni.$u.timeFormat(date, 'mm') * 1
	const day =  uni.$u.timeFormat(date, 'dd') * 1

	const arr = [{
		date: uni.$u.timeFormat(date, 'yyyy-mm-dd hh:MM:ss'),
		day: `${day}日`,
		monthandDay: `${month}月${day}日`,
		isActive: true,
		isHasTrajectory: false
	}]

	for (let i = 1; i <= daysAgo; i++) {
		let nextDate = new Date(date - 1000 * 60 * 60 * 24 * i)
		const month = uni.$u.timeFormat(nextDate, 'mm') * 1
		const day =  uni.$u.timeFormat(nextDate, 'dd') * 1
		
		arr.unshift({
			date: uni.$u.timeFormat(nextDate, 'yyyy-mm-dd') + ' 00:00:00',
			day: `${day}日`,
			monthandDay: `${month}月${day}日`,
			isActive: false,
			isHasTrajectory: false
		})
	}
	return arr
}

// 将分钟转换为小时和分钟
export const  minutesToHoursAndMinutes = (totalMinutes) => {
	const hours = Math.floor(totalMinutes/60)
	const minutes = totalMinutes % 60
	return {hours, minutes}
}


// 获取两个点经纬度的中心点
export const getCenterLonLat =(oneLon, oneLat, twoLon, twoLat) => {
	//oneLon：第一个点的经度；oneLat：第一个点的纬度；twoLon：第二个点的经度；twoLat：第二个点的纬度；
    let aLon = 0, aLat = 0;
    let bLon = Number(oneLon) - Number(twoLon);
    let bLat = Number(oneLat) - Number(twoLat);
    //Math.abs()绝对值
    if( bLon > 0){
        aLon = Number(oneLon) - Math.abs(bLon) / 2;
    }else{
        aLon = Number(twoLon) - Math.abs(bLon) / 2;
    }
    if( bLat > 0){
        aLat = Number(oneLat) - Math.abs(bLat) / 2;
    }else{
        aLat = Number(twoLat) - Math.abs(bLat) / 2;
    }
    return [aLon, aLat];
}
